package com.chat.http.controller;

import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.chat.http.service.PeopleRelationService;
import com.chat.http.service.PeopleService;
import com.chat.websocket.online.OnlineManage;
import com.chat.websocket.service.OneToOneChatService;

/**
 * 单聊相关请求controller
 * @author zh_zhong
 *
 */
@Controller
public class OnetoOneChatController {
	
	private HashMap<String, Object> map;
	
	@Autowired
	private PeopleRelationService peopleRelationService;
	
	@Autowired
	private OneToOneChatService oneToOneChatService;
	
	@Autowired
	private PeopleService peopleService;
	
	
	@RequestMapping("/test")
	public void test(){
	//	System.out.println(peopleRelationService.isFriend("13018749823", "13018749822") +"");
	//	System.out.println("is true?:" +peopleService.isExist("13019949823"));
		try{
			oneToOneChatService.sendRequestFriendShipMsg("110", "120");
		}catch(Exception e){
			e.printStackTrace();
		}
		
	}
	
	/**
	 * 请求添加好友
	 * @return
	 */
	@RequestMapping("/requestFriendShip")
	public @ResponseBody Map<String, Object> requestFriendShip(String userName, String toUserName)throws IOException{
		map = new HashMap <String, Object>();
		map.put("status", "error");
		
		if ((!peopleService.isExist(toUserName)) || peopleRelationService.isFriend(userName, toUserName)){
			map.put("msg", "请求添加的用户不存在或已是好友关系");
			return map;
		}
		
		if (OnlineManage.isOnlined(toUserName)){//在线
			oneToOneChatService.sendRequestFriendShipMsg(userName, toUserName);
			
		}else{//离线
			oneToOneChatService.saveRequestFriendShipMsg(userName, toUserName);
			
		}

		map.put("status", "success");
		return map;
	}
	
	/**
	 * 回复是否同意建立好友（双向建立好友关系）
	 * @param userName
	 * @param fromUserName
	 * @param isAgree 0-不同意，1-同意
	 * @return
	 */
	@RequestMapping("/responseFriendShip")
	public @ResponseBody Map<String, Object> responseFriendShip(String userName, String fromUserName,
			int isAgree)throws IOException{
		map = new HashMap <String, Object>();
		map.put("status", "error");
		
		if ((!peopleService.isExist(fromUserName)) || peopleRelationService.isFriend(userName, fromUserName)){
			map.put("msg", "请求来源的用户不存在或已是好友关系");
			return map;
		}
		
		if (isAgree == 1){//同意建立好友关系
			oneToOneChatService.buildFriendShip(userName, fromUserName);
		}
		
		//添加好友结果反馈通知
		oneToOneChatService.sendRequestFriendShipResult(fromUserName, userName, isAgree == 1);
		
		map.put("status", "success");
		return map;
				
	}
	
	
	/**
	 * 一对一聊天
	 */
	@RequestMapping("/oneToOnechat")
	public @ResponseBody Map<String, Object> oneToOnechat(String userName, String toUserName,
			String message, Integer msgType)throws IOException{
		map = new HashMap <String, Object>();
		map.put("status", "error");
		
		if ((!peopleService.isExist(toUserName))){
			map.put("msg", "用户不存在");
			return map;
		}
		
		if (OnlineManage.isOnlined(toUserName)){//在线
			
			oneToOneChatService.sendMsg(userName, toUserName, message, msgType);
		}else{//离线
			
			oneToOneChatService.saveUnsendMsg(userName, toUserName, message, msgType);
			
		}
		
		map.put("status", "success");
		return map;
	}
	
	/**
	 * 解除好友关系（双向解除）
	 * @param userName
	 * @param toUserName
	 */
	@RequestMapping("/removeFriendShip")
	public @ResponseBody Map removeFriendShip(String userName, String toUserName){
		map = new HashMap <String, Object>();
		map.put("status", "error");
		
		if ((!peopleService.isExist(toUserName))){
			map.put("msg", "用户不存在");
			return map;
		}
		
		oneToOneChatService.removeFriendShip(userName, toUserName);
		
		map.put("status", "success");
		return map;
		
	}
	

}
